package fr.webviewer.service;

import java.util.Date;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.preference.PreferenceManager;
import fr.webviewer.activity.PreferenceActivity;

public class TimeService extends Service{

	SharedPreferences preferences;
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		Context context = getBaseContext();
		preferences = PreferenceManager.getDefaultSharedPreferences(context);
		
		Date currentDate = new Date();
		Date morningFirst = new Date();
		Date morningSecond = new Date();
		Date eveningFirst = new Date();
		Date eveningSecond = new Date();
		Date tomorrowFirst = new Date();
		Date targetDate = new Date();

		morningFirst.setHours(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[0] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_HOURS_KEY, "0")));
		morningFirst.setMinutes(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[0] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_MINUTE_KEY, "0")));
		tomorrowFirst.setTime(morningFirst.getTime());
		tomorrowFirst.setDate(morningFirst.getDay() + 1);
		morningSecond.setHours(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[1] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_HOURS_KEY, "0")));
		morningSecond.setMinutes(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[1] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_MINUTE_KEY, "0")));

		eveningFirst.setHours(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[2] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_HOURS_KEY, "0")));
		eveningFirst.setMinutes(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[2] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_MINUTE_KEY, "0")));

		eveningSecond.setHours(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[3] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_HOURS_KEY, "0")));
		eveningSecond.setMinutes(Integer.parseInt(preferences.getString(
				PreferenceActivity.KEYS[3] + PreferenceActivity.TIME_KEY
						+ PreferenceActivity.TIME_MINUTE_KEY, "0")));
		if (currentDate.before(morningFirst)) {
			targetDate = morningFirst;
		} else if ((currentDate.before(morningSecond))) {
			targetDate = eveningFirst;
		} else if ((currentDate.before(eveningFirst))) {
			targetDate = eveningFirst;
		} else {
			targetDate = tomorrowFirst;
		}
		AlarmManager mgr = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		Intent i = new Intent(context, TrafficService.class);
		PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
		mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, targetDate.getTime(), pi);
	}
	
}
